package com.auto.learning.store.core;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import com.auto.learning.R;
import com.auto.learning.bean.AnchorBean;
import com.auto.learning.bean.AnchorBookBean;
import com.auto.learning.bean.BannerBean;
import com.auto.learning.bean.BannerItem;
import com.auto.learning.bean.Banner_H5;
import com.auto.learning.bean.BookBean;
import com.auto.learning.bean.BookListModel;
import com.auto.learning.bean.BookTypeBean;
import com.auto.learning.bean.BreakPointTable;
import com.auto.learning.bean.CacheTypeBean;
import com.auto.learning.bean.ListenStatusBean;
import com.auto.learning.bean.ReadLichengBean;
import com.auto.learning.bean.ReadModeBean;
import com.auto.learning.bean.SelectionVo;
import com.auto.learning.bean.UploadedImageBean;
import com.auto.learning.bean.UserBaseInfo;
import com.auto.learning.bean.UserInfoBean;
import com.auto.learning.bean.VideoBean;
import com.auto.learning.store.core.DatabaseUtil.OPERATION_TYPE;
import com.auto.learning.store.core.table.UserInfoTable;
import com.j256.ormlite.android.apptools.OrmLiteSqliteOpenHelper;
import com.j256.ormlite.support.ConnectionSource;
import com.j256.ormlite.table.TableUtils;

public class OrmliteDatabaseHelper extends OrmLiteSqliteOpenHelper {

	public OrmliteDatabaseHelper(Context context, String dbName, int version) {
		super(context, dbName, null, version, R.raw.model_config);
	}

	@Override
	public void onCreate(SQLiteDatabase db, ConnectionSource connectionSource) {
		try {
			Log.d("OrmliteDatabaseHelper", "onCreate");
			TableUtils.createTable(connectionSource, BookBean.class);
			TableUtils.createTable(connectionSource, SelectionVo.class);
			TableUtils.createTable(connectionSource, ReadModeBean.class);
			TableUtils.createTable(connectionSource, BannerBean.class);
			TableUtils.createTable(connectionSource, CacheTypeBean.class);
			TableUtils.createTable(connectionSource, BookTypeBean.class);
			TableUtils.createTable(connectionSource, UserInfoBean.class);
			TableUtils.createTable(connectionSource, UserBaseInfo.class);
			TableUtils.createTable(connectionSource, ReadLichengBean.class);
			TableUtils.createTable(connectionSource, VideoBean.class);
			TableUtils.createTable(connectionSource, AnchorBean.class);
			TableUtils.createTable(connectionSource, AnchorBookBean.class);
			TableUtils.createTable(connectionSource, ListenStatusBean.class);
			TableUtils.createTable(connectionSource, BannerItem.class);
			TableUtils.createTable(connectionSource, Banner_H5.class);
			TableUtils.createTable(connectionSource, BookListModel.class);
			TableUtils.createTable(connectionSource, UploadedImageBean.class);
			TableUtils.createTable(connectionSource, BreakPointTable.class);
			TableUtils.createTable(connectionSource, UserInfoTable.class);
		} catch (Exception e) {
			Log.e(OrmliteDatabaseHelper.class.getName(),"Can't create database", e);
			throw new RuntimeException(e);
		}
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, ConnectionSource connectionSource, int oldVersion, int newVersion) {
		try {
//			banner,book,read_licheng,t_anchor,t_listen_status,t_video,user,user_base_info,BookTypeBean
			DatabaseUtil.upgradeTable(db, connectionSource, BannerBean.class, OPERATION_TYPE.ADD);
			DatabaseUtil.upgradeTable(db, connectionSource, BookBean.class, OPERATION_TYPE.ADD);
			DatabaseUtil.upgradeTable(db, connectionSource, ReadLichengBean.class, OPERATION_TYPE.ADD);
			DatabaseUtil.upgradeTable(db, connectionSource, AnchorBean.class, OPERATION_TYPE.ADD);
			DatabaseUtil.upgradeTable(db, connectionSource, ListenStatusBean.class, OPERATION_TYPE.ADD);
			DatabaseUtil.upgradeTable(db, connectionSource, VideoBean.class, OPERATION_TYPE.ADD);
			DatabaseUtil.upgradeTable(db, connectionSource, UserInfoBean.class, OPERATION_TYPE.ADD);
			DatabaseUtil.upgradeTable(db, connectionSource, UserBaseInfo.class, OPERATION_TYPE.ADD);
			TableUtils.createTableIfNotExists(connectionSource, BannerItem.class);
			TableUtils.createTableIfNotExists(connectionSource, Banner_H5.class);
			TableUtils.createTableIfNotExists(connectionSource, UploadedImageBean.class);
			TableUtils.createTableIfNotExists(connectionSource, BookListModel.class);
			TableUtils.createTableIfNotExists(connectionSource, BreakPointTable.class);
			TableUtils.createTableIfNotExists(connectionSource, UserInfoTable.class);
			DatabaseUtil.upgradeTable(db, connectionSource, Banner_H5.class, OPERATION_TYPE.ADD);
			DatabaseUtil.upgradeTable(db, connectionSource, BookTypeBean.class, OPERATION_TYPE.ADD);
			
			
			/*Log.d("OrmliteDatabaseHelper", "onUpgrade");
			TableUtils.dropTable(connectionSource, BookBean.class, true);
			TableUtils.dropTable(connectionSource, SelectionVo.class, true);
			TableUtils.dropTable(connectionSource, ReadModeBean.class, true);
			TableUtils.dropTable(connectionSource, BannerBean.class, true);
			TableUtils.dropTable(connectionSource, CacheTypeBean.class, true);
			TableUtils.dropTable(connectionSource, BookTypeBean.class, true);
			TableUtils.dropTable(connectionSource, UserInfoBean.class, true);
			TableUtils.dropTable(connectionSource, UserBaseInfo.class, true);
			TableUtils.dropTable(connectionSource, ReadLichengBean.class, true);
			TableUtils.dropTable(connectionSource, VideoBean.class, true);
			onCreate(db, connectionSource);*/
		} catch (Exception e) {
			Log.e(OrmliteDatabaseHelper.class.getName(), "Can't drop databases", e);
			throw new RuntimeException(e);
		}
	}

}
